﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace _2DGame
{
    class Background
    {

        Vector2 Position;
        Texture2D Texture;
        SpriteFont jo;

        bool Collision; //used to check if cat has collieded with obsticle
        int StageWidth, StageHeight; //width and heigth of stage
        int CollisionPosition; //the position of the actual collition
        int Cpoint; // a variable which is used to count collisionPosition
        int respawnCounter = 0; //adds one to it when passing a respawn point
        int Name; //a number to see which backround slice we are dealing with

        //Constructor
        public Background(Texture2D texture, Vector2 position, int stageWidth, int stageHeight, int name)
        {
            Texture = texture;
            Position.X = position.X;
            StageWidth = stageWidth;
            StageHeight = stageHeight;
            CollisionPosition = -200;
            Name = name;
        }

        //to return a value to a temp textbox in  main
        public int colPos()
        {
            return CollisionPosition;
        }

        //to return a value to a temp textbox in  main
        public int name()
        {
            return Name;
        }

        //to return a value to a temp textbox in  main
        public Vector2 backPos()
        {
            return Position;
        }

        //to return a value to a temp textbox in  main
        public int spawnCounter()
        {
            return respawnCounter;
        }

        //Updates speed and position of background - Fredrik & Mats
        public void MoveBackground(GameTime gameTime, int speed)
        {
            //a function to check if collision ill be called before the cat moves
            Respawn();

            //makes the background move if collision = false
            if (Collision == false)
            {
                //makes the background move in speed of speedrunner function from levelcontroller
                Position.X -= LevelController.SpeedRunner();

                //sets the backround to zero and the level will run around and around...
                if (Position.X < (Name * 2000) - 18000)
                {

                    Position.X = (Name * 2000);
                }

            }

        }

        //Moving back to respawn point at collision - Fredrik & Mats
        public void Respawn()
        {
            //sets the actual x value of the obstacle
            Cpoint = Convert.ToInt32(Obstacle.positionDetection.X);

            // array which holds the respawn points
            int[] respawnPoint = new int[6];
            respawnPoint[0] = (Name * 2000) - 0;
            respawnPoint[1] = (Name * 2000) - 2900;
            respawnPoint[2] = (Name * 2000) - 4100;
            respawnPoint[3] = (Name * 2000) - 5400;
            respawnPoint[4] = (Name * 2000) - 6600;
            respawnPoint[5] = (Name * 2000) - 18000;

            //counts where the collision position is
            CollisionPosition = (Name * 2000) - Cpoint;

            //checks if collision with ostacle is true
            if (Obstacle.collisionDetection == true)
            {
                Collision = true;
            }

            //adds one to respawncounter when passing a respawn point 
            if (Position.X < respawnPoint[respawnCounter])
            {
                respawnCounter += 1;

            }


            if (Collision == true)
            {

                Position.X += 5; //Speed of going back to respawn point

                //when cat hits respawnpoin it goes back to normal
                if (Position.X >= respawnPoint[respawnCounter - 1])
                {
                    Collision = false;
                    Obstacle.collisionDetection = false;
                }

            }
        }

        //Updates - Fredrik & Mats
        public void Update(GameTime gameTime, int speed)
        {
        }

        //Texture properties
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(Texture, Position, Color.White);
            spriteBatch.End();
        }
    }
}
